package com.ly.oa.action.admin;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Type;
import java.math.BigDecimal;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.ly.core.command.QueryFilter;
import com.ly.core.web.action.BaseAction;


import com.ly.oa.model.admin.DepreRecord;
import com.ly.oa.model.admin.FixedAssets;
import com.ly.oa.service.admin.DepreRecordService;
import com.ly.oa.service.admin.FixedAssetsService;

import flexjson.DateTransformer;
import flexjson.JSONSerializer;
/**
 * 
 * @author csx
 *
 */
public class FixedAssetsAction extends BaseAction{
	@Resource
	private FixedAssetsService fixedAssetsService;
	private FixedAssets fixedAssets;
	
	@Resource
	private DepreRecordService depreRecordService;
	
	private Long assetsId;

	public Long getAssetsId() {
		return assetsId;
	}

	public void setAssetsId(Long assetsId) {
		this.assetsId = assetsId;
	}

	public FixedAssets getFixedAssets() {
		return fixedAssets;
	}

	public void setFixedAssets(FixedAssets fixedAssets) {
		this.fixedAssets = fixedAssets;
	}

	/**
	 * 显示列表
	 */
	public String list(){
		
		QueryFilter filter=new QueryFilter(getRequest());
		List<FixedAssets> list= fixedAssetsService.getAll(filter);
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
		.append(filter.getPagingBean().getTotalItems()).append(",result:");
		JSONSerializer serializer=new JSONSerializer();
		serializer.transform(new DateTransformer("yyyy-MM-dd HH:mm:ss"),"buyDate");
		serializer.transform(new DateTransformer("yyyy-MM-dd HH:mm:ss"),"startDepre");
		serializer.transform(new DateTransformer("yyyy-MM-dd HH:mm:ss"),"manuDate");
		buff.append(serializer.exclude(new String[]{"class"}).prettyPrint(list));
		buff.append("}");
		
		jsonString=buff.toString();
		
		return SUCCESS;
	}
	/**
	 * 批量删除
	 * @return
	 */
	public String multiDel(){
		
		String[]ids=getRequest().getParameterValues("ids");
		if(ids!=null){
			for(String id:ids){
				fixedAssetsService.remove(new Long(id));
			}
		}
		
		jsonString="{success:true}";
		
		return SUCCESS;
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String get(){
		FixedAssets fixedAssets=fixedAssetsService.get(assetsId);
		StringBuffer sb = new StringBuffer("{success:true,data:");
		JSONSerializer serializer=new JSONSerializer();
		serializer.transform(new DateTransformer("yyyy-MM-dd HH:mm:ss"),"buyate");
		serializer.transform(new DateTransformer("yyyy-MM-dd HH:mm:ss"),"manuDate");
		serializer.transform(new DateTransformer("yyyy-MM-dd HH:mm:ss"),"startDepre");
		sb.append(serializer.exclude(new String[]{"class"}).prettyPrint(fixedAssets));
		sb.append("}");
		setJsonString(sb.toString());
		
		return SUCCESS;
	}
	/**
	 * 添加及保存操作
	 */
	public String save(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss-SSSS");//自动生成商品号
	    if(fixedAssets.getAssetsId()==null){
	    	fixedAssets.setAssetsNo(sdf.format(new Date()));
	    	fixedAssets.setAssetCurValue(fixedAssets.getAssetValue());
	    }
		fixedAssetsService.save(fixedAssets);
		setJsonString("{success:true}");
		return SUCCESS;
	}

}
